#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    loadData();
}

MainWindow::~MainWindow()
{
    delete database;
}

void MainWindow::setupUI()
{
    treeWidget = new QTreeWidget(this);
    treeWidget->setHeaderLabels(QStringList() << "层级" << "编号" << "名称" << "创建时间" << "修改时间" << "描述");
    setCentralWidget(treeWidget);

    // 右键菜单
    treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(treeWidget, &QTreeWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
        QMenu menu;
        menu.addAction("新建", this, &MainWindow::onNew);
        menu.addAction("删除", this, &MainWindow::onDeleteItem);
        menu.addAction("修改", this, &MainWindow::onEditItem);
        menu.exec(treeWidget->viewport()->mapToGlobal(pos));
    });
}

void MainWindow::loadData()
{
    database = new Database();
    // 加载产品数据
    auto products = database->getProducts();
    for (const auto &product : products) {
        QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
        item->setText(0, "产品");
        item->setText(1, product.product_id);
        item->setText(2, product.name);
        item->setText(3, product.created_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(4, product.updated_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(5, product.description);
    }

    // 加载系统数据
    auto systems = database->getSystems();
    for (const auto &system : systems) {
        QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
        item->setText(0, "系统");
        item->setText(1, system.system_id);
        item->setText(2, system.name);
        item->setText(3, system.created_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(4, system.updated_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(5, system.description);
    }

    // 加载组件数据
    auto components = database->getComponents();
    for (const auto &component : components) {
        QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
        item->setText(0, "组件");
        item->setText(1, component.component_id);
        item->setText(2, component.name);
        item->setText(3, component.created_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(4, component.updated_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(5, component.description);
    }

    // 加载零件数据
    auto parts = database->getParts();
    for (const auto &part : parts) {
        QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
        item->setText(0, "零件");
        item->setText(1, part.part_id);
        item->setText(2, part.name);
        item->setText(3, part.created_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(4, part.updated_at.toString("yyyy-MM-dd hh:mm:ss"));
        item->setText(5, part.description);
    }
}

void MainWindow::onNew(){
    NewItemDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted) {
        QString type = dialog.getType();
        QString id = dialog.getId();
        QString name = dialog.getName();
        QString description = dialog.getDescription();
        if (type=="产品")       database->addProduct(id, name, description);
        else if (type=="系统")  database->addSystem(id, name, description);
        else if (type=="组件")  database->addComponent(id, name, description);
        else if (type=="零件")  database->addPart(id, name, description);
        loadData();
    }
}

void MainWindow::onDeleteItem() {
    QTreeWidgetItem *item = treeWidget->currentItem();
    if (!item) return;

    QString type = item->text(0);
    QString id = item->text(1);

    if (type == "产品") {
        database->deleteProduct(id);
    } else if (type == "系统") {
        database->deleteSystem(id);
    } else if (type == "组件") {
        database->deleteComponent(id);
    } else if (type == "零件") {
        database->deletePart(id);
    }

    loadData();
}

void MainWindow::onEditItem() {
    QTreeWidgetItem *item = treeWidget->currentItem();
    if (!item) return;

    QString type = item->text(0);
    QString id = item->text(1);
    QString name = item->text(2);
    QString description = item->text(5);

    NewItemDialog dialog(this);
    dialog.setWindowTitle("修改");

    // 设置当前值
    dialog.setId(id);
    dialog.setName(name);
    dialog.setDescription(description);

    if (dialog.exec() == QDialog::Accepted) {
        QString newId = dialog.getId();
        QString newName = dialog.getName();
        QString newDescription = dialog.getDescription();

        if (type == "产品") {
            database->updateProduct(id, newName, newDescription);
        } else if (type == "系统") {
            database->updateSystem(id, newName, newDescription);
        } else if (type == "组件") {
            database->updateComponent(id, newName, newDescription);
        } else if (type == "零件") {
            database->updatePart(id, newName, newDescription);
        }

        loadData();
    }
}